﻿Option Strict On
Option Explicit On

Imports CObject
Imports ConnectionServer
Imports System.Data.SqlClient
Imports Objects.Utility

Namespace CMT

    Public Class Meeting
        Inherits CObject.CObject

#Region "Constants"
        Public Const AttendeeNonAvailabilityScheduleDisplayText As String = "Not available"
        Public Const BookedScheduleTooltip As String = ""
        Public Const BookedScheduleDisplayText As String = "Booked for another meeting."
        Public Const MeetingScheduleItemID As String = "schMeeting"
        Public Const MeetingScheduleBlockedItemID As String = "schAttendeeMeeting"
        Public Const RoomScheduleBlockedItemID As String = "schRoom"
        Public Const MeetingScheduleAvailableItemID As String = "schAttendeeMeetingAvl"
        Public Const OutOfMeetingTimeRangeErrorMessage As String = "Selected time range is out of meeting time range, please select another time range."
        Public Const AttendeeOutOfMeetingTimeRangeErrorMessage As String = "Attendee time range is out of meeting time range, please select another time range."
        Public Const AttndeeNotAvailableErrorMessage As String = "Attendee is not available on this time range, please pick another time range."
        Public Const MeetingTimeSlotError As String = "Meeting end time should be greater than start time."
        Public Const AttendeeTimeSlotError As String = "Attendee end time should be greater than start time."
        Public Const MeetingTimeSlotNotGreaterThan45 As String = "Meeting time span should be atleast for 15 minutes."
        Public Const MeetingNoAttendeeError As String = "Please select atleast one attendee to schedule the meeting."
        Public Const MeetingDateOutOfEventDate As String = "Meeting date/time is out of event date/time range, please select an appropriate date and time range."
        Public Const MeetingDateLessThanCurrentDate As String = "Meeting date less than current date, please select another date."
        Public Const NoAttendeesForEvent As String = "There are no attendees for the selected event, please add attendee for the event."
        Public Const NoRoomsForEvent As String = "There are no rooms for the selected event, please add rooms for the event."
        Public Const NoRoomsAvailableForMeeting As String = "The selected room is not available for the meeting schedule, please select another room or reschedule meeting."
        Public Const MeetingAddedSuccessfully As String = "Meeting added successfully for the event."
        Public Const MeetingUpdatedSuccessfully As String = "Meeting updated successfully."
        Public Const MeetingAddedFailed As String = "Failed to add the meeting for the event."
        Public Const MeetingUpdationFailed As String = "Failed to update the meeting."
        Public Const AllAttendeeNotScheduled As String = "All the selected attendee are not scheduled for the meeting, please schedule."
        Public Const FailedToAddAttendee As String = "Failed to add all/some of the meeting attendee."
        Public Const FailedToUpdateAttendee As String = "Failed to update all/some of the meeting attendee."
        Public Const MeetingNameCannotBeBlank As String = "Meeting name cannot be blank, please provide meeting name."
        Public Const MeetingMailSent As String = "Mail sent to meeting attendee."
        Public Const MeetingMailSentFailed As String = "Mail sending failed due to some issues."
        Public Const MeetingDateChnagedError As String = "Please schedule meeting and then save."
        Public Const MeetingTypeError As String = "Meeting type cannot be changed to Roundtable once the meeting is scheduled, please cancel the meeting and schedule new one."

#End Region
        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal piMeetingID As Integer)
            MyBase.New(piMeetingID)
        End Sub

        Public Overrides Function Clone() As CObject.CObject
            Return New Meeting
        End Function

        Public Overrides ReadOnly Property Key As String
            Get
                Return "MeetingID"
            End Get
        End Property

        Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String
            Select Case peCommandType
                Case CommandType.SQLSelect
                    Return "EXEC CMT_GetSpecificMeetingDetails {MeetingID}"
                Case CommandType.SQLInsert
                    Return "EXEC CMT_MeetingDetailsAdd {EventID}, {MeetingName}, {MeetingDescr}, {MeetingNote}, {MeetingTypeID}, {StartDate}, {EndDate}, {RoomID}, {AdditionalRoomReq}, {MeetingLink}, {FeedBackMailSent}, {MeetingDate}, {ClientName}, {MeetingID} "
                Case CommandType.SQLUpdate
                    Return "EXEC CMT_MeetingDetailsUpdate  {MeetingID}, {MeetingName}, {MeetingDescr}, {MeetingNote}, {MeetingTypeID}, {StartDate}, {EndDate}, {RoomID}, {AdditionalRoomReq}, {MeetingLink}, {MeetingDate}, {ClientName}, {MeetingStatusID}"
                Case Else
                    Throw New SystemException("Invalid Command Type")
            End Select
        End Function

        Public Overrides ReadOnly Property SourceDataBase As ConnectionServer.Connection.SourceDataBase
            Get
                Return Connection.SourceDataBase.CMT
            End Get
        End Property

#Region "Properties"

        Public Property MeetingID As Integer
            'Get
            '    Return CastToInteger(GetProperty("MeetingID"))
            'End Get
            Get
                Return CastToInteger(GetProperty("MeetingID"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("MeetingID", value)
            End Set
        End Property

        Public Property EventID As Integer
            Get
                Return CastToInteger(GetProperty("EventID"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("EventID", value)
            End Set
        End Property

        Public Property FeedBackMailSent As Boolean
            Get
                Return CastToBoolean(GetProperty("FeedBackMailSent"))
            End Get
            Set(ByVal value As Boolean)
                SetProperty("FeedBackMailSent", value)
            End Set
        End Property

        Public ReadOnly Property EventObj As CMTEvent
            Get
                Dim loObj As CMTEvent = New CMTEvent(EventID)
                If loObj.EventID <> 0 Then
                    Return loObj
                Else
                    'no data
                    Return Nothing
                End If
            End Get
        End Property

        Public Property MeetingName As String
            Get
                Return CastToString(GetProperty("MeetingName"))
            End Get
            Set(ByVal value As String)
                SetProperty("MeetingName", value)
            End Set
        End Property

        Public Property MeetingDescr As String
            Get
                Return CastToString(GetProperty("MeetingDescr"))
            End Get
            Set(ByVal value As String)
                SetProperty("MeetingDescr", value)
            End Set
        End Property

        Public Property MeetingNote As String
            Get
                Return CastToString(GetProperty("MeetingNote"))
            End Get
            Set(ByVal value As String)
                SetProperty("MeetingNote", value)
            End Set
        End Property

        Public Property MeetingTypeID As Integer
            Get
                Return CastToInteger(GetProperty("MeetingTypeID"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("MeetingTypeID", value)
            End Set
        End Property

        Public ReadOnly Property MeetingTypeObj As MeetingType
            Get
                Dim loObj As MeetingType = New MeetingType(MeetingTypeID)
                If loObj.MeetingTypeID <> 0 Then
                    Return loObj
                Else
                    'no data
                    Return Nothing
                End If
            End Get
        End Property

        Public Property MeetingDate As Date
            Get
                Return CastToDate(GetProperty("MeetingDate"))
            End Get
            Set(ByVal value As Date)
                SetProperty("MeetingDate", value)
            End Set
        End Property

        Public Property StartDate As String
            Get
                Return CastToString(GetProperty("StartDate"))
            End Get
            Set(ByVal value As String)
                SetProperty("StartDate", value)
            End Set
        End Property

        Public Property EndDate As String
            Get
                Return CastToString(GetProperty("EndDate"))
            End Get
            Set(ByVal value As String)
                SetProperty("EndDate", value)
            End Set
        End Property

        Public Property MeetingStatusID As Integer
            Get
                Return CastToInteger(GetProperty("MeetingStatusID"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("MeetingStatusID", value)
            End Set
        End Property

        Public ReadOnly Property MeetingStatusObj As MeetingStatus
            Get
                Dim loObj As MeetingStatus = New MeetingStatus(MeetingStatusID)
                If loObj.MeetingStatusID <> 0 Then
                    Return loObj
                Else
                    'no data
                    Return Nothing
                End If
            End Get
        End Property

        Public Property RoomID As Integer
            Get
                Return CastToInteger(GetProperty("RoomID"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("RoomID", value)
            End Set
        End Property

        Public ReadOnly Property MeetingRoom As Room
            Get
                Dim loObj As Room = New Room(RoomID)
                If loObj.RoomID <> 0 Then
                    Return loObj
                Else
                    'no data
                    Return Nothing
                End If
            End Get
        End Property

        Public Property AdditionalRoomReq As String
            Get
                Return CastToString(GetProperty("AdditionalRoomReq"))
            End Get
            Set(ByVal value As String)
                SetProperty("AdditionalRoomReq", value)
            End Set
        End Property

        Public Property MeetingLink As String
            Get
                Return CastToString(GetProperty("MeetingLink"))
            End Get
            Set(ByVal value As String)
                SetProperty("MeetingLink", value)
            End Set
        End Property

        Public Property ClientName As String
            Get
                Return CastToString(GetProperty("ClientName"))
            End Get
            Set(ByVal value As String)
                SetProperty("ClientName", value)
            End Set
        End Property

#End Region

#Region "Methods"

        ''' <summary>
        ''' Method to sent mail to meeting participant(s).
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetMailDetails(ByVal pMeetingId As Integer) As Boolean
            Dim loDT As DataTable
            Dim mailSent As Boolean
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim lsSQl As String = String.Empty
                lsSQl = "EXEC CMT_Mail_MeetingDetails " + pMeetingId.ToString()
                loDT = loConn.GetDataTable(lsSQl)
            End Using
            mailSent = SendMailToAttendee(loDT)
            Return mailSent
        End Function

        Public Function SendMailToAttendee(ByVal meetingDetails As DataTable) As Boolean
            Try
                Dim loSetting As Settings = New Settings(Connection.SourceDataBase.CMT)
                Dim loEmail As Email = New Email(CInt(loSetting("CMT-MeetingNotificationEmailId").ToString))
                Dim liCount As Integer
                Dim Subject As String
                Dim Body As String
                Dim tempBody As String
                Subject = loEmail.Subject
                Body = loEmail.BodyText

                With loEmail

                    For liCount = 0 To (meetingDetails.Rows.Count - 1)
                        .Subject = Subject.Replace("~Subject", meetingDetails.Rows(liCount)("Subject").ToString)

                        tempBody = String.Empty
                        tempBody = Body.Replace("~Name", meetingDetails.Rows(liCount)("Name").ToString)

                        .BodyText = tempBody.Replace("~Body", " " + meetingDetails.Rows(liCount)("Body").ToString)

                        loEmail.SendEmail(meetingDetails.Rows(liCount)("To").ToString)
                    Next

                End With

                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

        ''' <summary>
        ''' Method to get the meeting details for search meeting.
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetSearchedMeetingDetails(ByVal pEventId As String, ByVal pMeetingTypeID As String, ByVal pRoomID As String, ByVal pMeetingName As String, ByVal pMeetingDate As String, ByVal pMeetingStatusId As String, ByVal pCurrentUserGUI As String) As DataTable
            Dim dsSearchedMeeting As DataSet = New DataSet()
            Dim dtSearchedMeeting As DataTable = New DataTable()
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim loCommand As SqlCommand = New SqlCommand
                'set the connection
                loCommand.Connection = loConn.Connection

                ' Set the parameters
                loCommand.Parameters.Add("@pEventID", SqlDbType.Int)
                loCommand.Parameters.Add("@pMeetingName", SqlDbType.NVarChar, 100)
                loCommand.Parameters.Add("@pMeetingTypeID", SqlDbType.Int)
                loCommand.Parameters.Add("@pMeetingDate", SqlDbType.DateTime)
                loCommand.Parameters.Add("@pRoomID", SqlDbType.Int)
                loCommand.Parameters.Add("@pMeetingStatusId", SqlDbType.Int)
                loCommand.Parameters.Add("@pEventAdminId", SqlDbType.NVarChar, 100)

                loCommand.Parameters("@pEventID").Value = pEventId
                loCommand.Parameters("@pMeetingName").Value = pMeetingName
                loCommand.Parameters("@pMeetingTypeID").Value = pMeetingTypeID
                loCommand.Parameters("@pMeetingDate").Value = pMeetingDate
                loCommand.Parameters("@pRoomID").Value = pRoomID
                loCommand.Parameters("@pMeetingStatusId").Value = pMeetingStatusId
                loCommand.Parameters("@pEventAdminId").Value = pCurrentUserGUI

                loCommand.CommandType = Data.CommandType.StoredProcedure
                loCommand.CommandText = "CMT_MeetingSearch"

                Dim da As SqlDataAdapter = New SqlDataAdapter(loCommand)
                da.Fill(dsSearchedMeeting)
                If (dsSearchedMeeting.Tables.Count > 0) Then dtSearchedMeeting = dsSearchedMeeting.Tables(0)
                loConn.Connection.Close()
                Return dtSearchedMeeting
            End Using
        End Function
#End Region

    End Class

    Public Class MeetingType
        Inherits CObject.CObject

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal piMeetingTypeID As Integer)
            MyBase.New(piMeetingTypeID)
        End Sub

        Public Overrides Function Clone() As CObject.CObject
            Return New MeetingType
        End Function

        Public Overrides ReadOnly Property Key As String
            Get
                Return "MeetingTypeID"
            End Get
        End Property

        Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String
            Select Case peCommandType
                Case CommandType.SQLSelect
                    Return "EXEC CMT_GetMeetingTypes"
                Case Else
                    Throw New SystemException("Invalid Command Type")
            End Select
        End Function

        Public Overrides ReadOnly Property SourceDataBase As ConnectionServer.Connection.SourceDataBase
            Get
                Return Connection.SourceDataBase.CMT
            End Get
        End Property

#Region "Properties"

        Public ReadOnly Property MeetingTypeID As Integer
            Get
                Return CastToInteger(GetProperty("MeetingTypeID"))
            End Get
        End Property

        Public Property MeetingType As String
            Get
                Return CastToString(GetProperty("MeetingType"))
            End Get
            Set(ByVal value As String)
                SetProperty("MeetingType", value)
            End Set
        End Property

#End Region

    End Class

    Public Class MeetingStatus
        Inherits CObject.CObject

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal piMeetingStatusID As Integer)
            MyBase.New(piMeetingStatusID)
        End Sub

        Public Overrides Function Clone() As CObject.CObject
            Return New MeetingStatus()
        End Function

        Public Overrides ReadOnly Property Key As String
            Get
                Return "MeetingServiceID"
            End Get
        End Property

        Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String
            Select Case peCommandType
                Case CommandType.SQLSelect
                    Return "EXEC CMT_GetMeetingStatus"
                Case Else
                    Throw New SystemException("Invalid Command Type")
            End Select
        End Function

        Public Overrides ReadOnly Property SourceDataBase As ConnectionServer.Connection.SourceDataBase
            Get
                Return Connection.SourceDataBase.CMT
            End Get
        End Property

#Region "Properties"

        Public ReadOnly Property MeetingStatusID As Integer
            Get
                Return CastToInteger(GetProperty("MeetingStatusID"))
            End Get
        End Property

        Public Property MeetingStatus As String
            Get
                Return CastToString(GetProperty("MeetingStatus"))
            End Get
            Set(ByVal value As String)
                SetProperty("MeetingStatus", value)
            End Set
        End Property

#End Region

    End Class

    Public Class MeetingMail
        Inherits CObject.CObject

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal piMeetingID As Integer)
            MyBase.New(piMeetingID)
        End Sub

        Public Overrides Function Clone() As CObject.CObject
            Return New MeetingMail()
        End Function

        Public Overrides ReadOnly Property Key As String
            Get
                Return "MeetingMailID"
            End Get
        End Property

        Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String
            Select Case peCommandType
                Case CommandType.SQLSelect
                    Return "EXEC CMT_Mail_MeetingDetails {MeetingID}, NULL, {MeetingType} "
                Case Else
                    Throw New SystemException("Invalid Command Type")
            End Select
        End Function

        Public Overrides ReadOnly Property SourceDataBase As ConnectionServer.Connection.SourceDataBase
            Get
                Return Connection.SourceDataBase.CMT
            End Get
        End Property

#Region "Properties"

        Public Property MeetingID As Integer
            Get
                Return CastToInteger(GetProperty("MeetingID"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("MeetingID", value)
            End Set
        End Property

        Public Property MeetingType As String
            Get
                Return CastToString(GetProperty("MeetingType"))
            End Get
            Set(ByVal value As String)
                SetProperty("MeetingType", value)
            End Set
        End Property

        Public Property Subject As String
            Get
                Return CastToString(GetProperty("Subject"))
            End Get
            Set(ByVal value As String)
                SetProperty("Subject", value)
            End Set
        End Property

        Public Property Name As String
            Get
                Return CastToString(GetProperty("Name"))
            End Get
            Set(ByVal value As String)
                SetProperty("Name", value)
            End Set
        End Property

        Public Property Body As String
            Get
                Return CastToString(GetProperty("Body"))
            End Get
            Set(ByVal value As String)
                SetProperty("Body", value)
            End Set
        End Property

        Public Property MailTo As String
            Get
                Return CastToString(GetProperty("To"))
            End Get
            Set(ByVal value As String)
                SetProperty("To", value)
            End Set
        End Property

#End Region

    End Class

End Namespace

